//
// proba.cpp for 204param in /home/morel_t//Rendu/maths/204param
// 
// Made by thomas morel
// Login   <morel_t@epitech.net>
// 
// Started on  Tue Mar 27 17:30:29 2012 thomas morel
// Last update Wed Mar 28 11:36:38 2012 thomas morel
//

#include	"204param.hh"

double		esperance(const t_param *data)
{
  double	h;
  double	i;
  double	j;

  i = 0.f;
  j = 0.f;
  h = ((data->max - data->min) / ITER);
  for (double inc = 1.f; inc < ITER; inc++)
    i += (data->min + inc * h) * data->fct(data->min + inc * h);
  i *= 2.f;
  for (double inc = 0.f; inc < ITER; inc++)
    j += (data->min + inc * h + h / 2.f) * data->fct(data->min + inc * h + h / 2.f);
  j *= 4.f;
  return (((data->max - data->min) / (6.f * ITER)) *
	  (data->min * data->fct(data->min) + data->max * data->fct(data->max) + i + j));
}

double		variance(const t_param *data)
{
  double        h;
  double        i;
  double        j;
  double        m;

  i = 0.f;
  j = 0.f;
  m = esperance(data);
  h = ((data->max - data->min) / ITER);
  for (double inc = 1.f; inc < ITER; inc++)
    i += (((data->min + inc * h) - m) * ((data->min + inc * h) - m)) * data->fct(data->min + inc * h);
  i *= 2.f;
  for (double inc = 0.f; inc < ITER; inc++)
    j += (((data->min + inc * h + h / 2.f) - m) * ((data->min + inc * h + h / 2.f) - m)) * data->fct(data->min + inc * h + h / 2.f);
  j *= 4.f;
  return (((data->max - data->min) / (6.f * ITER)) *
	  ((data->min - m) * data->fct(data->min) + (data->max - m) * data->fct(data->max) + i + j));
}
